package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 预约设置
 */
@RestController
@RequestMapping(path = "/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 根据时间查询预约设置
     *
     * @return
     */
    @GetMapping(path = "/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(@RequestParam("date") String date) {
        try {
            List<Map<String, String>> mapList = this.orderSettingService.getOrderSettingsByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            //调用工具类解析数据
            List<String[]> readExcel = POIUtils.readExcel(multipartFile);
            //判断是否有数据
            if (readExcel != null && readExcel.size() > 0) {
                //有数据
                List<OrderSetting> orderSettingList = readExcel.stream().map(strings -> {
                    OrderSetting orderSetting = null;
                    try {
                        orderSetting = new OrderSetting(DateUtils.parseString2Date(strings[0]), Integer.parseInt(strings[1]));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return orderSetting;
                }).collect(Collectors.toList());
                this.orderSettingService.upload(orderSettingList);
            }
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /*@RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(),
                    OrderSetting.class,
                    new OrderSettingListener(orderSettingService))
                    .sheet().doRead();
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_FAIL);
        }
    }*/

    /**
     * 更新预约人数
     *
     * @param orderSetting
     * @return
     */
    @PutMapping("updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting) {
        try {
            this.orderSettingService.updateOrderSettingNumber(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
